package com.hotel.api.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

public class UploadFile {


    public static String getFilePath(MultipartFile file) {
        //保存图片的路径
        String filePath = "D:/upload/images/";
        //获取原始图片的拓展名
        String fileName = file.getOriginalFilename();
        //新的文件名字
        fileName = UUID.randomUUID().toString().replace("-", "")
                + fileName.substring(fileName.lastIndexOf("."));
        //封装上传文件位置的全路径
        File targetFile = new File(filePath, fileName);

        try {
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            file.transferTo(targetFile);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
